package src.leetcode.backtrack.medium_0046;

import javax.swing.text.StyledEditorKit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class permute {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.permute(new int[]{1, 2, 3}));
    }
}

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path;
    public List<List<Integer>> permute(int[] nums) {
        path = new ArrayList<>(nums.length);
        boolean []used = new boolean[nums.length];
        dfs(nums, used);
        return res;
    }

    private void dfs(int []nums, boolean []used){
        if(nums.length == path.size()) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length ; i++){

            if(used[i]) continue;
            used[i] = true;
            path.add(nums[i]);
            dfs(nums, used);
            path.remove(path.size() - 1);
            used[i] = false;
        }
    }
}